  require 'rexml/document'
  
  def convert_xml_to_csv(file_name)
      # This is the new CSV file handler.
      file_hdlr = File.new("#{file_name.to_s.split("\.").first}.csv","w")
      # This is a header file handler.
      header_hdlr = File.new("#{file_name.to_s.split("\.").first}_header.csv","w")
      # names of the columns will stored in this string.
      header_row = String.new
      repeat_counter = 0
      #XML file is opened.
      orders = REXML::Document.new(File.new("#{file_name}"))
      orders.root.each_element do |order|
          # The initial string for generating the single CSV rows.
          single_row = String.new
          order.each_element do |node|
              if node.has_elements?
                  node.each_element do |child|
                      # Inserting a value
                      single_row << "\""+"#{child.text}"+"\","
                      # Writing the column name to the file only once.
                      header_row << child.name+"," if repeat_counter == 0
                  end
              else
                  # Node has no furthur children so inserting just its value.  
                  single_row << "\"#{node.text}\","
                  header_row << node.name+"," if repeat_counter == 0
              end    
          end
          # Setting the last , as null  
          single_row[(single_row.length-1)] = ""
          # Writing the CSV row to a file.
          file_hdlr.write(single_row)
          file_hdlr.write("\n")
          repeat_counter = 1
      end      
      # Same process for header file.  
      header_row[(header_row.length-1)] = ""  
      header_hdlr.write(header_row)  
      # closing the file connections.
      file_hdlr.close
      header_hdlr.close
  end 
  
  
  
  def convert_fixed_width_to_csv(file_name)
      # This is the handler for header file.
      header_hdlr = File.new("#{file_name.to_s.split("\.").first}_header.csv","w")
      # This is the handler for new CSV data file.
      new_file_hdlr = File.new("#{file_name.to_s.split("\.").first}.csv","w")
      # This is the old file handler for curren fixed width file.
      file_hdlr = File.open("#{file_name.to_s.gsub("\\","/")}","r")
      all_values = []
      # Taking all the rows in an array of arrays.
      file_hdlr.readlines.each{|value|
          all_values << value.split(" ")
      }
      # First row is to be of column rows.
      csv_file_length = (all_values.sort.reverse[0].length)
      header_hdlr.write(all_values.sort.reverse[0].join(","))
      # Traversing the array of arrays.
      for row_counter in 1..(all_values.length-1)
          arr_value = all_values[row_counter]
          # SIngel row is the initial string for the CSV row
          single_row = String.new
          # This loop generates a CSV rwo from the given fixed width row.
          for counter in 0..(csv_file_length-1)
              if arr_value[counter].nil?
                  single_row << " "
              else
                  single_row << "\"#{arr_value[counter]}\""
              end  
              if counter != (csv_file_length-1)
                  single_row << ","
              end  
          end
          # Writing the data to the CSV.  
          new_file_hdlr.write(single_row)
          new_file_hdlr.write("\n")
      end
      # Closing all the file handlers.  
      file_hdlr.close
      header_hdlr.close
      new_file_hdlr.close
  end      
                    
  